<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
        <el-button type="primary" :size="size" icon="el-icon-plus" @click="addCompany">新增企业</el-button>
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'

export default {
  name: 'company',
  mixins: [mixinTable],
  data(vm) {
    return {
      tableInnerBtns: [
        {
          name: '编辑',
          icon: 'el-icon-edit',
          handle(row) {
            vm.routerLink(`/member/company/detail/${row.id}`)
          }
        }
      ],
      // 表格内操作按钮
      tableHeader: [
        {
          label: '所属区域',
          prop: 'companyCityName'
        },
        {
          label: '所属企业',
          prop: 'companyName'
        },
        {
          label: '企业法人',
          prop: 'linkmanName'
        },
        {
          label: '法人电话',
          prop: 'linkmanPhone'
        },
        {
          label: '企业地址',
          prop: 'companyAddress'
        },
        {
          label: '车况检查',
          prop: 'isForceImg',
          formatter: row => (row && row.isForceImg === 1 ? '强制' : '非强制'),
          setColor: row => (row.isForceImg === 1 ? 'red' : '')
        },
        // {
        //   label: '启用状态',
        //   prop: 'status',
        //   formatter: row => row && vm.setTableColumnLabel(row.status, 'disabledStatus')
        // },
        {
          label: '合作开始时间',
          prop: 'joinStart',
          width: 160
        },
        {
          label: '合作结束时间',
          prop: 'joinEnd',
          width: 160
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    addCompany() {
      this.routerLink('/member/company/detail')
    },
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime)
      this.isLoading = true
      this.$api.member
        .fetch('companyList', {
          ...searchDate,
          ...other,
          rows: pageNo,
          offset: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    }
  }
}
</script>
